package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.bean.Order;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        //获取页面输入的手机号
        String telephone = (String) map.get("telephone");
        //获取页面输入的验证码
        String validateCode = (String) map.get("validateCode");
        //从Redis中获取缓存的验证码
        String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //校验验证码
        if (redisCode == null || !redisCode.equals(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result = null;
        map.put("orderType",Order.ORDERTYPE_WEIXIN);
        result = orderService.submit(map);
        return result;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Map map = orderService.findById(id);
        return new Result(true,MessageConstant.ORDER_SUCCESS,map);
    }
}
